"""自定义异常类"""

from typing import Optional, Any, Dict


class MedicalReportAPIException(Exception):
    """基础异常类"""

    def __init__(
        self,
        code: str,
        message: str,
        data: Optional[Any] = None,
        status_code: int = 400
    ):
        self.code = code
        self.message = message
        self.data = data
        self.status_code = status_code
        super().__init__(self.message)


class ParameterException(MedicalReportAPIException):
    """参数异常"""

    def __init__(self, message: str, data: Optional[Any] = None):
        super().__init__(
            code="20001",
            message=message,
            data=data,
            status_code=400
        )


class AuthenticationException(MedicalReportAPIException):
    """认证异常"""

    def __init__(self, message: str = "认证失败", data: Optional[Any] = None):
        super().__init__(
            code="50001",
            message=message,
            data=data,
            status_code=401
        )


class AuthorizationException(MedicalReportAPIException):
    """授权异常"""

    def __init__(self, message: str = "无权访问", data: Optional[Any] = None):
        super().__init__(
            code="50001",
            message=message,
            data=data,
            status_code=403
        )


class NotFoundException(MedicalReportAPIException):
    """资源不存在异常"""

    def __init__(self, message: str = "资源不存在", data: Optional[Any] = None):
        super().__init__(
            code="10002",
            message=message,
            data=data,
            status_code=404
        )


class SystemException(MedicalReportAPIException):
    """系统异常"""

    def __init__(self, message: str = "系统错误", data: Optional[Any] = None):
        super().__init__(
            code="10003",
            message=message,
            data=data,
            status_code=500
        )


class DatabaseException(MedicalReportAPIException):
    """数据库异常"""

    def __init__(self, message: str = "数据库操作异常", data: Optional[Any] = None):
        super().__init__(
            code="30001",
            message=message,
            data=data,
            status_code=500
        )


class BusinessException(MedicalReportAPIException):
    """业务异常"""

    def __init__(self, code: str, message: str, data: Optional[Any] = None):
        super().__init__(
            code=code,
            message=message,
            data=data,
            status_code=400
        )


def create_exception_by_code(code: str, message: Optional[str] = None, data: Optional[Any] = None) -> MedicalReportAPIException:
    """根据错误码创建对应的异常"""
    from .error_codes import get_error_message

    if not message:
        message = get_error_message(code)

    # 根据错误码前缀判断异常类型
    if code.startswith("2"):
        return ParameterException(message, data)
    elif code.startswith("3"):
        return DatabaseException(message, data)
    elif code.startswith("5"):
        return AuthorizationException(message, data)
    elif code == "10002":
        return NotFoundException(message, data)
    elif code == "10003":
        return SystemException(message, data)
    else:
        return BusinessException(code, message, data)